package com.idea.plugin.core.resolver;

import com.github.javaparser.Range;
import com.github.javaparser.ast.expr.BooleanLiteralExpr;
import com.github.javaparser.ast.expr.Expression;
import com.github.javaparser.ast.stmt.IfStmt;
import com.idea.plugin.core.logging.Log;
import com.idea.plugin.core.logging.SlfLog;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Optional;

/**
 * @author yanzhao
 * @date 2022/2/22 16:50
 * @since 1.0.0
 */
public class IfStmtHandler {

    private static Log log = new SlfLog(LoggerFactory.getLogger(IfStmtHandler.class));

    public static void handle(IfStmt ifStmt, String method, List<String> ifMethods) {

        method = method + "If";
        Expression condition = ifStmt.getCondition();
        if (condition.isBooleanLiteralExpr()) { // if (true)
            final BooleanLiteralExpr booleanLiteralExpr = (BooleanLiteralExpr) condition;
            method = method + booleanLiteralExpr.getValue() + "Test";
            handleMethodConflict(ifStmt, condition, method, ifMethods);
        } else if (condition.isNameExpr()) { // Boolean b = false; if (b)
            System.out.println();
        } else if (condition.isMethodCallExpr()) { // if (test()) if (Boolean.TRUE.equals(true))
            System.out.println();
        } else if (condition.isBinaryExpr()) { // if (list.isEmpty() && i == 0) if (i > 0 && i == 0)
            System.out.println();
        }
//        if (condition instanceof BooleanLiteralExpr) { // if (true)
//
//        } else if (condition instanceof BinaryExpr) { // if (list.isEmpty() && i == 0) if (i > 0 && i == 0)
//            System.out.println();
//        } else if (condition instanceof MethodCallExpr) { // if (test()) if (Boolean.TRUE.equals(true))
//            System.out.println();
//        } else if (condition instanceof NameExpr) { // Boolean b = false; if (b)
//            System.out.println();
//        }
    }

    public static void handleMethodConflict(IfStmt ifStmt, Expression condition, String method, List<String> methods) {
        int i = 1;
        String dynamicMethod = method;
        while (methods.contains(dynamicMethod)) {
            dynamicMethod = method + i;
            i++;
        }
        methods.add(dynamicMethod);
        Optional<Range> range = condition.getRange();
        if (range.isPresent()) {
            log.info(range.get().toString());
        }
    }

}
